package gov.tax.rti.base.util;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.bstek.dorado.core.resource.ResourceManager;
import com.bstek.dorado.core.resource.ResourceManagerUtils;
import com.bstek.dorado.util.DateUtils;

public class DateUtil {
	
	public static Date getDefaultSxsj() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(2100, 0, 1,0,0,0);
		return calendar.getTime();
		//return new Date(2100, 1, 1);
	}

	public static void resetHMS(Calendar calendar) {
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
	}

	public static void setLastHMS(Calendar calendar) {
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
	}

	public static Calendar getFirstDayOfDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar;
	}

	public static Calendar getFirstTimeOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		resetHMS(calendar);
		return calendar;
	}
	
	public static Calendar getLastTimeOfDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		setLastHMS(calendar);
		return calendar;
	}

	// 获取当前月
	public static int getMonth(Date date) {
		int month = Integer.valueOf(DateUtils.format("MM", date));
		return month;
	}

	// 获取当前半年的字符
	public static int getHalfYear(Date date) {
		int halfYear = (int) Math.floor(getMonth(date) / 7);
		return halfYear;
	}

	// 获取当前季度的字符
	public static int getSeason(Date date) {
		int season = (int) Math.ceil(getMonth(date) / 3.0);
		return season;
	}

	// 获取年、半年、季度、月份的上期和去年同期的字符串
	public static Map<String, Object> getPreAndLast(Object dateText)
			throws Exception {

		Map<String, Object> preAndLastMap = new HashMap<String, Object>();

		String sq = "";
		String qntq = "";
		try {
			if (dateText instanceof String) {

				String dateStr = (String) dateText;
				int length = dateStr.length();

				if (dateStr.indexOf("-") > -1) {
					Date date = DateUtils.parse("yyyy-MM", dateStr);
					sq = getLastMonth("code", date);
					qntq = getPreMonth("code", date);
				} else {
					switch (length) {
					case 4:
						int year = Integer.valueOf(dateStr);
						sq = String.valueOf(year - 1);
						qntq = sq;
						break;
					case 5:
						year = Integer
								.valueOf(dateStr.substring(0, length - 1));
						int halfYear = Integer.valueOf(dateStr
								.substring(length - 1));
						if (halfYear == 0) {
							sq = String.valueOf(year - 1).concat("1");
						} else {
							sq = String.valueOf(year).concat("0");
						}
						qntq = String.valueOf(year - 1).concat(
								String.valueOf(halfYear));
						break;
					case 6:
						year = Integer
								.valueOf(dateStr.substring(0, length - 2));
						halfYear = Integer.valueOf(dateStr.substring(
								length - 2, length - 1));
						int season = Integer.valueOf(dateStr
								.substring(length - 1));
						int lastSeason = season - 1;
						if (lastSeason == 0) {
							sq = String.valueOf(year - 1).concat("14");
						} else if (lastSeason >= 3) {
							sq = String.valueOf(year).concat("1")
									.concat(String.valueOf(lastSeason));
						} else {
							sq = String.valueOf(year).concat("0")
									.concat(String.valueOf(lastSeason));
						}
						qntq = String.valueOf(year - 1).concat(
								dateStr.substring(4));
						break;
					default:
						throw new RuntimeException("不合法的格式！");
					}
				}

				preAndLastMap.put("SQ", sq);
				preAndLastMap.put("QNTQ", qntq);

			} else if (dateText instanceof Date) {
				Date date = (Date) dateText;
				sq = getLastMonth("code", date);
				qntq = getPreMonth("code", date);
				preAndLastMap.put("SQ", DateUtils.parse("yyyy-MM", sq));
				preAndLastMap.put("QNTQ", DateUtils.parse("yyyy-MM", qntq));
			}

			return preAndLastMap;
		} catch (ParseException e) {
			throw new RuntimeException("不合法的年月格式！必须为yyyy-MM格式。");
		}

	}

	/* 年 */

	// 获取年字符串
	public static String getYearString(Calendar calendar, String returnType) {
		if (StringUtils.equals(returnType, "code")
				|| StringUtils.isEmpty(returnType)) {
			return DateUtils.format("yyyy", calendar.getTime());
		} else if (StringUtils.equals(returnType, "start")) {
			resetHMS(calendar);
			calendar.set(Calendar.MONTH, 0);
			return DateUtils.format("yyyy-MM-dd HH:mm:ss", calendar.getTime());
		} else if (StringUtils.equals(returnType, "end")) {
			setLastHMS(calendar);
			calendar.set(Calendar.MONTH, 11);
			calendar.roll(Calendar.DAY_OF_MONTH, false);
			return DateUtils.format("yyyy-MM-dd HH:mm:ss", calendar.getTime());
		} else {
			return null;
		}
	}

	// 获取年code的label
	public static String getYearCodeLabel(String code) {
		String label = ResourceManagerUtils.get(
				"dorado/resources/i18n/dropdown").getString("ndp_base_year");
		return code.concat(label);
	}

	// 今年
	public static String getYear(String returnType) {
		return getYear(returnType, new Date());
	}

	public static String getYear(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		return getYearString(calendar, returnType);
	}

	public static String getYearLabel() {
		String code = getYear("code");
		return getYearCodeLabel(code);
	}

	// 去年
	public static String getLastYear(String returnType) {
		return getLastYear(returnType, new Date());
	}

	public static String getLastYear(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.add(Calendar.YEAR, -1);
		return getYearString(calendar, returnType);
	}

	public static String getLastYearLabel() {
		String code = getLastYear("code");
		return getYearCodeLabel(code);
	}

	// 明年
	public static String getNextYear(String returnType) {
		return getNextYear(returnType, new Date());
	}

	public static String getNextYear(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.add(Calendar.YEAR, 1);
		return getYearString(calendar, returnType);
	}

	public static String getNextYearLabel() {
		String code = getNextYear("code");
		return getYearCodeLabel(code);
	}

	/* 半年 */

	// 获取半年字符串
	public static String getHalfYearString(Calendar calendar, String returnType) {
		int halfYear = getHalfYear(calendar.getTime());
		if (StringUtils.equals(returnType, "code")
				|| StringUtils.isEmpty(returnType)) {
			return getYearString(calendar, returnType).concat(
					String.valueOf(halfYear));
		} else if (StringUtils.equals(returnType, "start")) {
			resetHMS(calendar);
			calendar.set(Calendar.MONTH, halfYear * 6);
			return DateUtils.format("yyyy-MM-dd HH:mm:ss", calendar.getTime());
		} else if (StringUtils.equals(returnType, "end")) {
			setLastHMS(calendar);
			calendar.set(Calendar.MONTH, (halfYear + 1) * 6 - 1);
			calendar.roll(Calendar.DAY_OF_MONTH, false);
			return DateUtils.format("yyyy-MM-dd HH:mm:ss", calendar.getTime());
		} else {
			return null;
		}
	}

	// 获取半年的label
	public static String getHalfYearCodeLabel(String code) {
		int length = code.length();
		int halfYearCode = Integer.parseInt(code.substring(length - 1));
		String yearCode = code.substring(0, length - 1);
		ResourceManager rm = ResourceManagerUtils
				.get("dorado/resources/i18n/dropdown");
		String yearLabel = rm.getString("ndp_base_year");
		String halfYearLabel = rm.getString("ndp_base_halfyear").split(",")[halfYearCode];
		return yearCode.concat(yearLabel).concat(halfYearLabel);
	}

	// 当期
	public static String getCurrentHalfYear(String returnType) {
		return getCurrentHalfYear(returnType, new Date());
	}

	public static String getCurrentHalfYear(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		return getHalfYearString(calendar, returnType);
	}

	public static String getCurrentHalfYearLabel() {
		String code = getCurrentHalfYear("code");
		return getHalfYearCodeLabel(code);
	}

	// 今年上半年
	public static String getFirstHalfYear(String returnType) {
		return getFirstHalfYear(returnType, new Date());
	}

	public static String getFirstHalfYear(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.set(Calendar.MONTH, Calendar.JANUARY);
		return getHalfYearString(calendar, returnType);
	}

	public static String getFirstHalfYearLabel() {
		String code = getFirstHalfYear("code");
		return getHalfYearCodeLabel(code);
	}

	// 今年下半年
	public static String getSecondHalfYear(String returnType) {
		return getSecondHalfYear(returnType, new Date());
	}

	public static String getSecondHalfYear(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.set(Calendar.MONTH, Calendar.DECEMBER);
		return getHalfYearString(calendar, returnType);
	}

	public static String getSecondHalfYearLabel() {
		String code = getSecondHalfYear("code");
		return getHalfYearCodeLabel(code);
	}

	// 上期
	public static String getLastHalfYear(String returnType) {
		return getLastHalfYear(returnType, new Date());
	}

	public static String getLastHalfYear(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.add(Calendar.MONTH, -6);
		return getHalfYearString(calendar, returnType);
	}

	public static String getLastHalfYearLabel() {
		String code = getLastHalfYear("code");
		return getHalfYearCodeLabel(code);
	}

	// 下期
	public static String getNextHalfYear(String returnType) {
		return getNextHalfYear(returnType, new Date());
	}

	public static String getNextHalfYear(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.add(Calendar.MONTH, 6);
		return getHalfYearString(calendar, returnType);
	}

	public static String getNextHalfYearLabel() {
		String code = getNextHalfYear("code");
		return getHalfYearCodeLabel(code);
	}

	// 去年同期
	public static String getPreHalfYear(String returnType) {
		return getPreHalfYear(returnType, new Date());
	}

	public static String getPreHalfYear(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.add(Calendar.MONTH, -12);
		return getHalfYearString(calendar, returnType);
	}

	public static String getPreHalfYearLabel() {
		String code = getPreHalfYear("code");
		return getHalfYearCodeLabel(code);
	}

	// 明年同期
	public static String getSubHalfYear(String returnType) {
		return getSubHalfYear(returnType, new Date());
	}

	public static String getSubHalfYear(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.add(Calendar.MONTH, 12);
		return getHalfYearString(calendar, returnType);
	}

	public static String getSubHalfYearLabel() {
		String code = getSubHalfYear("code");
		return getHalfYearCodeLabel(code);
	}

	/* 季度 */

	// 获取季度字符串
	public static String getSeasonString(Calendar calendar, String returnType) {
		int season = getSeason(calendar.getTime());
		if (StringUtils.equals(returnType, "code")
				|| StringUtils.isEmpty(returnType)) {
			return getHalfYearString(calendar, returnType).concat(
					String.valueOf(season));
		} else if (StringUtils.equals(returnType, "start")) {
			resetHMS(calendar);
			calendar.set(Calendar.MONTH, (season - 1) * 3);
			return DateUtils.format("yyyy-MM-dd HH:mm:ss", calendar.getTime());
		} else if (StringUtils.equals(returnType, "end")) {
			setLastHMS(calendar);
			calendar.set(Calendar.MONTH, season * 3 - 1);
			calendar.roll(Calendar.DAY_OF_MONTH, false);
			return DateUtils.format("yyyy-MM-dd HH:mm:ss", calendar.getTime());
		} else {
			return null;
		}
	}

	// 获取季度的label
	public static String getSeasonCodeLabel(String code) {
		int length = code.length();
		int seasonCode = Integer.parseInt(code.substring(length - 1));
		String yearCode = code.substring(0, length - 2);
		ResourceManager rm = ResourceManagerUtils
				.get("dorado/resources/i18n/dropdown");
		String yearLabel = rm.getString("ndp_base_year");
		String seasonLabel = rm.getString("ndp_base_season").split(",")[seasonCode - 1];
		return yearCode.concat(yearLabel).concat(seasonLabel);
	}

	// 当期
	public static String getCurrentSeason(String returnType) {
		return getCurrentSeason(returnType, new Date());
	}

	public static String getCurrentSeason(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		return getSeasonString(calendar, returnType);
	}

	public static String getCurrentSeasonLabel() {
		String code = getCurrentSeason("code");
		return getSeasonCodeLabel(code);
	}

	// 今年一季度
	public static String getFirstSeason(String returnType) {
		return getFirstSeason(returnType, new Date());
	}

	public static String getFirstSeason(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.set(Calendar.MONTH, Calendar.JANUARY);
		return getSeasonString(calendar, returnType);
	}

	public static String getFirstSeasonLabel() {
		String code = getFirstSeason("code");
		return getSeasonCodeLabel(code);
	}

	// 今年二季度
	public static String getSecondSeason(String returnType) {
		return getSecondSeason(returnType, new Date());
	}

	public static String getSecondSeason(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.set(Calendar.MONTH, Calendar.APRIL);
		return getSeasonString(calendar, returnType);
	}

	public static String getSecondSeasonLabel() {
		String code = getSecondSeason("code");
		return getSeasonCodeLabel(code);
	}

	// 今年三季度
	public static String getThirdSeason(String returnType) {
		return getThirdSeason(returnType, new Date());
	}

	public static String getThirdSeason(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.set(Calendar.MONTH, Calendar.JULY);
		return getSeasonString(calendar, returnType);
	}

	public static String getThirdSeasonLabel() {
		String code = getThirdSeason("code");
		return getSeasonCodeLabel(code);
	}

	// 今年四季度
	public static String getFourthSeason(String returnType) {
		return getFourthSeason(returnType, new Date());
	}

	public static String getFourthSeason(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.set(Calendar.MONTH, Calendar.OCTOBER);
		return getSeasonString(calendar, returnType);
	}

	public static String getFourthSeasonLabel() {
		String code = getFourthSeason("code");
		return getSeasonCodeLabel(code);
	}

	// 上期
	public static String getLastSeason(String returnType) {
		return getLastSeason(returnType, new Date());
	}

	public static String getLastSeason(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.add(Calendar.MONTH, -3);
		return getSeasonString(calendar, returnType);
	}

	public static String getLastSeasonLabel() {
		String code = getLastSeason("code");
		return getSeasonCodeLabel(code);
	}

	// 下期
	public static String getNextSeason(String returnType) {
		return getNextSeason(returnType, new Date());
	}

	public static String getNextSeason(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.add(Calendar.MONTH, 3);
		return getSeasonString(calendar, returnType);
	}

	public static String getNextSeasonLabel() {
		String code = getNextSeason("code");
		return getSeasonCodeLabel(code);
	}

	// 去年同期
	public static String getPreSeason(String returnType) {
		return getPreSeason(returnType, new Date());
	}

	public static String getPreSeason(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.add(Calendar.MONTH, -12);
		return getSeasonString(calendar, returnType);
	}

	public static String getPreSeasonLabel() {
		String code = getPreSeason("code");
		return getSeasonCodeLabel(code);
	}

	// 明年同期
	public static String getSubSeason(String returnType) {
		return getSubSeason(returnType, new Date());
	}

	public static String getSubSeason(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.add(Calendar.MONTH, 12);
		return getSeasonString(calendar, returnType);
	}

	public static String getSubSeasonLabel() {
		String code = getSubSeason("code");
		return getSeasonCodeLabel(code);
	}

	/* 年月 */

	// 获取returnType是start和end的年月字符串
	public static String getYMString(Calendar calendar, String returnType) {
		if (StringUtils.equals(returnType, "code")
				|| StringUtils.isEmpty(returnType)) {
			return DateUtils.format("yyyy-MM", calendar.getTime());
		} else if (StringUtils.equals(returnType, "start")) {
			resetHMS(calendar);
			return DateUtils.format("yyyy-MM-dd HH:mm:ss", calendar.getTime());
		} else if (StringUtils.equals(returnType, "end")) {
			setLastHMS(calendar);
			calendar.roll(Calendar.DAY_OF_MONTH, false);
			return DateUtils.format("yyyy-MM-dd HH:mm:ss", calendar.getTime());
		} else {
			return null;
		}
	}

	// 获取年月的label
	public static String getYMCodeLabel(String code) {
		ResourceManager rm = ResourceManagerUtils
				.get("dorado/resources/i18n/dropdown");
		String yearLabel = rm.getString("ndp_base_year");
		String yMLabel = rm.getString("ndp_base_month");
		return code.replace("-", yearLabel).concat(yMLabel);
	}

	// 当月
	public static String getCurrentMonth(String returnType) {
		return getCurrentMonth(returnType, new Date());
	}

	public static String getCurrentMonth(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		return getYMString(calendar, returnType);
	}

	public static String getCurrentMonthLabel() {
		String code = getCurrentMonth("code");
		return getYMCodeLabel(code);
	}

	// 上月
	public static String getLastMonth(String returnType) {
		return getLastMonth(returnType, new Date());
	}

	public static String getLastMonth(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.add(Calendar.MONTH, -1);
		return getYMString(calendar, returnType);
	}

	public static String getLastMonthLabel() {
		String code = getLastMonth("code");
		return getYMCodeLabel(code);
	}

	// 下月
	public static String getNextMonth(String returnType) {
		return getNextMonth(returnType, new Date());
	}

	public static String getNextMonth(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.add(Calendar.MONTH, 1);
		return getYMString(calendar, returnType);
	}

	public static String getNextMonthLabel() {
		String code = getNextMonth("code");
		return getYMCodeLabel(code);
	}

	// 本季初
	public static String getCurrentSeasonFirstMonth(String returnType) {
		return getCurrentSeasonFirstMonth(returnType, new Date());
	}

	public static String getCurrentSeasonFirstMonth(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		int season = getSeason(calendar.getTime());
		calendar.set(Calendar.MONTH, (season - 1) * 3);
		return getYMString(calendar, returnType);
	}

	public static String getCurrentSeasonFirstMonthLabel() {
		String code = getCurrentSeasonFirstMonth("code");
		return getYMCodeLabel(code);
	}

	// 本季末
	public static String getCurrentSeasonLastMonth(String returnType) {
		return getCurrentSeasonLastMonth(returnType, new Date());
	}

	public static String getCurrentSeasonLastMonth(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		int season = getSeason(calendar.getTime());
		calendar.set(Calendar.MONTH, season * 3 - 1);
		return getYMString(calendar, returnType);
	}

	public static String getCurrentSeasonLastMonthLabel() {
		String code = getCurrentSeasonLastMonth("code");
		return getYMCodeLabel(code);
	}

	// 本半年初
	public static String getCurrentHalfYearFirstMonth(String returnType) {
		return getCurrentHalfYearFirstMonth(returnType, new Date());
	}

	public static String getCurrentHalfYearFirstMonth(String returnType,
			Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		int halfYear = getHalfYear(calendar.getTime());
		calendar.set(Calendar.MONTH, halfYear * 6);
		return getYMString(calendar, returnType);
	}

	public static String getCurrentHalfYearFirstMonthLabel() {
		String code = getCurrentHalfYearFirstMonth("code");
		return getYMCodeLabel(code);
	}

	// 本半年末
	public static String getCurrentHalfYearLastMonth(String returnType) {
		return getCurrentHalfYearLastMonth(returnType, new Date());
	}

	public static String getCurrentHalfYearLastMonth(String returnType,
			Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		int halfYear = getHalfYear(calendar.getTime());
		calendar.set(Calendar.MONTH, (halfYear + 1) * 6 - 1);
		return getYMString(calendar, returnType);
	}

	public static String getCurrentHalfYearLastMonthLabel() {
		String code = getCurrentHalfYearLastMonth("code");
		return getYMCodeLabel(code);
	}

	// 今年1月
	public static String getYearFirstMonth(String returnType) {
		return getYearFirstMonth(returnType, new Date());
	}

	public static String getYearFirstMonth(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.set(Calendar.MONTH, Calendar.JANUARY);
		return getYMString(calendar, returnType);
	}

	public static String getYearFirstMonthLabel() {
		String code = getYearFirstMonth("code");
		return getYMCodeLabel(code);
	}

	// 今年12月
	public static String getYearLastMonth(String returnType) {
		return getYearLastMonth(returnType, new Date());
	}

	public static String getYearLastMonth(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.set(Calendar.MONTH, Calendar.DECEMBER);
		return getYMString(calendar, returnType);
	}

	public static String getYearLastMonthLabel() {
		String code = getYearLastMonth("code");
		return getYMCodeLabel(code);
	}

	// 去年同期
	public static String getPreMonth(String returnType) {
		return getPreMonth(returnType, new Date());
	}

	public static String getPreMonth(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.add(Calendar.YEAR, -1);
		return getYMString(calendar, returnType);
	}

	public static String getPreMonthLabel() {
		String code = getPreMonth("code");
		return getYMCodeLabel(code);
	}

	// 明年同期
	public static String getSubMonth(String returnType) {
		return getSubMonth(returnType, new Date());
	}

	public static String getSubMonth(String returnType, Date date) {
		Calendar calendar = getFirstDayOfDate(date);
		calendar.add(Calendar.YEAR, 1);
		return getYMString(calendar, returnType);
	}

	public static String getSubMonthLabel() {
		String code = getSubMonth("code");
		return getYMCodeLabel(code);
	}

	/* 日期多选 */

	// 今天
	public static String getToday() {
		return getToday("yyyy-MM-dd", new Date());
	}

	public static String getToday(String format) {
		return getToday(format, new Date());
	}

	public static String getToday(String format, Date date) {
		return DateUtils.format(format, date);
	}

	// 昨日
	public static String getYesterday() {
		return getYesterday("yyyy-MM-dd", new Date());
	}

	public static String getYesterday(String format) {
		return getYesterday(format, new Date());
	}

	public static String getYesterday(String format, Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return DateUtils.format(format, calendar.getTime());
	}

	// 明日
	public static String getTomorrow() {
		return getTomorrow("yyyy-MM-dd", new Date());
	}

	public static String getTomorrow(String format) {
		return getTomorrow(format, new Date());
	}

	public static String getTomorrow(String format, Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		return DateUtils.format(format, calendar.getTime());
	}

	// 本月初
	public static String getCurrentMonthFirstDay() {
		return getCurrentMonthFirstDay("yyyy-MM-dd", new Date());
	}

	public static String getCurrentMonthFirstDay(String format) {
		return getCurrentMonthFirstDay(format, new Date());
	}

	public static String getCurrentMonthFirstDay(String format, Date date) {
		Calendar calendar = getFirstTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return DateUtils.format(format, calendar.getTime());
	}

	// 本月末
	public static String getCurrentMonthLastDay() {
		return getCurrentMonthLastDay("yyyy-MM-dd", new Date());
	}

	public static String getCurrentMonthLastDay(String format) {
		return getCurrentMonthLastDay(format, new Date());
	}

	public static String getCurrentMonthLastDay(String format, Date date) {
		Calendar calendar = getLastTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.roll(Calendar.DAY_OF_MONTH, false);
		return DateUtils.format(format, calendar.getTime());
	}

	// 上月初
	public static String getLastMonthFirstDay() {
		return getLastMonthFirstDay("yyyy-MM-dd", new Date());
	}

	public static String getLastMonthFirstDay(String format) {
		return getLastMonthFirstDay(format, new Date());
	}

	public static String getLastMonthFirstDay(String format, Date date) {
		Calendar calendar = getFirstTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, -1);
		return DateUtils.format(format, calendar.getTime());
	}

	// 上月末
	public static String getLastMonthLastDay() {
		return getLastMonthLastDay("yyyy-MM-dd", new Date());
	}

	public static String getLastMonthLastDay(String format) {
		return getLastMonthLastDay(format, new Date());
	}

	public static String getLastMonthLastDay(String format, Date date) {
		Calendar calendar = getLastTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, -1);
		calendar.roll(Calendar.DAY_OF_MONTH, false);
		return DateUtils.format(format, calendar.getTime());
	}

	// 下月初
	public static String getNextMonthFirstDay() {
		return getNextMonthFirstDay("yyyy-MM-dd", new Date());
	}

	public static String getNextMonthFirstDay(String format) {
		return getNextMonthFirstDay(format, new Date());
	}

	public static String getNextMonthFirstDay(String format, Date date) {
		Calendar calendar = getFirstTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, 1);
		return DateUtils.format(format, calendar.getTime());
	}

	// 下月末
	public static String getNextMonthLastDay() {
		return getNextMonthLastDay("yyyy-MM-dd", new Date());
	}

	public static String getNextMonthLastDay(String format) {
		return getNextMonthLastDay(format, new Date());
	}

	public static String getNextMonthLastDay(String format, Date date) {
		Calendar calendar = getLastTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, 1);
		calendar.roll(Calendar.DAY_OF_MONTH, false);
		return DateUtils.format(format, calendar.getTime());
	}

	// 本季初
	public static String getCurrentSeasonFirstDay() {
		return getCurrentSeasonFirstDay("yyyy-MM-dd", new Date());
	}

	public static String getCurrentSeasonFirstDay(String format) {
		return getCurrentSeasonFirstDay(format, new Date());
	}

	public static String getCurrentSeasonFirstDay(String format, Date date) {
		Calendar calendar = getFirstTimeOfDay(date);
		int season = getSeason(calendar.getTime());
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.MONTH, (season - 1) * 3);
		return DateUtils.format(format, calendar.getTime());
	}

	// 本季末
	public static String getCurrentSeasonLastDay() {
		return getCurrentSeasonLastDay("yyyy-MM-dd", new Date());
	}

	public static String getCurrentSeasonLastDay(String format) {
		return getCurrentSeasonLastDay(format, new Date());
	}

	public static String getCurrentSeasonLastDay(String format, Date date) {
		Calendar calendar = getLastTimeOfDay(date);
		int season = getSeason(calendar.getTime());
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.MONTH, season * 3 - 1);
		calendar.roll(Calendar.DAY_OF_MONTH, false);
		return DateUtils.format(format, calendar.getTime());
	}

	// 上季初
	public static String getLastSeasonFirstDay() {
		return getLastSeasonFirstDay("yyyy-MM-dd", new Date());
	}

	public static String getLastSeasonFirstDay(String format) {
		return getLastSeasonFirstDay(format, new Date());
	}

	public static String getLastSeasonFirstDay(String format, Date date) {
		Calendar calendar = getFirstTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, -3);
		int season = getSeason(calendar.getTime());
		calendar.set(Calendar.MONTH, (season - 1) * 3);
		return DateUtils.format(format, calendar.getTime());
	}

	// 上季末
	public static String getLastSeasonLastDay() {
		return getLastSeasonLastDay("yyyy-MM-dd", new Date());
	}

	public static String getLastSeasonLastDay(String format) {
		return getLastSeasonLastDay(format, new Date());
	}

	public static String getLastSeasonLastDay(String format, Date date) {
		Calendar calendar = getLastTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, -3);
		int season = getSeason(calendar.getTime());
		calendar.set(Calendar.MONTH, season * 3 - 1);
		calendar.roll(Calendar.DAY_OF_MONTH, false);
		return DateUtils.format(format, calendar.getTime());
	}

	// 下季初
	public static String getNextSeasonFirstDay() {
		return getNextSeasonFirstDay("yyyy-MM-dd", new Date());
	}

	public static String getNextSeasonFirstDay(String format) {
		return getNextSeasonFirstDay(format, new Date());
	}

	public static String getNextSeasonFirstDay(String format, Date date) {
		Calendar calendar = getFirstTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, 3);
		int season = getSeason(calendar.getTime());
		calendar.set(Calendar.MONTH, (season - 1) * 3);
		return DateUtils.format(format, calendar.getTime());
	}

	// 下季末
	public static String getNextSeasonLastDay() {
		return getNextSeasonLastDay("yyyy-MM-dd", new Date());
	}

	public static String getNextSeasonLastDay(String format) {
		return getNextSeasonLastDay(format, new Date());
	}

	public static String getNextSeasonLastDay(String format, Date date) {
		Calendar calendar = getLastTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, 3);
		int season = getSeason(calendar.getTime());
		calendar.set(Calendar.MONTH, season * 3 - 1);
		calendar.roll(Calendar.DAY_OF_MONTH, false);
		return DateUtils.format(format, calendar.getTime());
	}

	// 本半年初
	public static String getCurrentHalfYearFirstDay() {
		return getCurrentHalfYearFirstDay("yyyy-MM-dd", new Date());
	}

	public static String getCurrentHalfYearFirstDay(String format) {
		return getCurrentHalfYearFirstDay(format, new Date());
	}

	public static String getCurrentHalfYearFirstDay(String format, Date date) {
		Calendar calendar = getFirstTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		int halfYear = getHalfYear(calendar.getTime());
		calendar.set(Calendar.MONTH, halfYear * 6);
		return DateUtils.format(format, calendar.getTime());
	}

	// 本半年末
	public static String getCurrentHalfYearLastDay() {
		return getCurrentHalfYearLastDay("yyyy-MM-dd", new Date());
	}

	public static String getCurrentHalfYearLastDay(String format) {
		return getCurrentHalfYearLastDay(format, new Date());
	}

	public static String getCurrentHalfYearLastDay(String format, Date date) {
		Calendar calendar = getLastTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		int halfYear = getHalfYear(calendar.getTime());
		calendar.set(Calendar.MONTH, (halfYear + 1) * 6 - 1);
		calendar.roll(Calendar.DAY_OF_MONTH, false);
		return DateUtils.format(format, calendar.getTime());
	}

	// 本年初
	public static String getCurrentYearFirstDay() {
		return getCurrentYearFirstDay("yyyy-MM-dd", new Date());
	}

	public static String getCurrentYearFirstDay(String format) {
		return getCurrentYearFirstDay(format, new Date());
	}

	public static String getCurrentYearFirstDay(String format, Date date) {
		Calendar calendar = getFirstTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.MONTH, Calendar.JANUARY);
		return DateUtils.format(format, calendar.getTime());
	}

	// 本年末
	public static String getCurrentYearLastDay() {
		return getCurrentYearLastDay("yyyy-MM-dd", new Date());
	}

	public static String getCurrentYearLastDay(String format) {
		return getCurrentYearLastDay(format, new Date());
	}

	public static String getCurrentYearLastDay(String format, Date date) {
		Calendar calendar = getLastTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.MONTH, Calendar.DECEMBER);
		calendar.roll(Calendar.DAY_OF_MONTH, false);
		return DateUtils.format(format, calendar.getTime());
	}

	// 去年同日
	public static String getLastYearToday() {
		return getLastYearToday("yyyy-MM-dd", new Date());
	}

	public static String getLastYearToday(String format) {
		return getLastYearToday(format, new Date());
	}

	public static String getLastYearToday(String format, Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, -1);
		return DateUtils.format(format, calendar.getTime());
	}

	// 去年初
	public static String getLastYearFirstDay() {
		return getLastYearFirstDay("yyyy-MM-dd", new Date());
	}

	public static String getLastYearFirstDay(String format) {
		return getLastYearFirstDay(format, new Date());
	}

	public static String getLastYearFirstDay(String format, Date date) {
		Calendar calendar = getFirstTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.MONTH, Calendar.JANUARY);
		calendar.add(Calendar.YEAR, -1);
		return DateUtils.format(format, calendar.getTime());
	}

	// 去年末
	public static String getLastYearLastDay() {
		return getLastYearLastDay("yyyy-MM-dd", new Date());
	}

	public static String getLastYearLastDay(String format) {
		return getLastYearLastDay(format, new Date());
	}

	public static String getLastYearLastDay(String format, Date date) {
		Calendar calendar = getLastTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.MONTH, Calendar.DECEMBER);
		calendar.roll(Calendar.DAY_OF_MONTH, false);
		calendar.add(Calendar.YEAR, -1);
		return DateUtils.format(format, calendar.getTime());
	}

	// 去年同月初
	public static String getLastYearCurrentMonthFirstDay() {
		return getLastYearCurrentMonthFirstDay("yyyy-MM-dd", new Date());
	}

	public static String getLastYearCurrentMonthFirstDay(String format) {
		return getLastYearCurrentMonthFirstDay(format, new Date());
	}

	public static String getLastYearCurrentMonthFirstDay(String format,
			Date date) {
		Calendar calendar = getFirstTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.YEAR, -1);
		return DateUtils.format(format, calendar.getTime());
	}

	// 去年同月末
	public static String getLastYearCurrentMonthLastDay() {
		return getLastYearCurrentMonthLastDay("yyyy-MM-dd", new Date());
	}

	public static String getLastYearCurrentMonthLastDay(String format) {
		return getLastYearCurrentMonthLastDay(format, new Date());
	}

	public static String getLastYearCurrentMonthLastDay(String format, Date date) {
		Calendar calendar = getLastTimeOfDay(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.YEAR, -1);
		calendar.roll(Calendar.DAY_OF_MONTH, false);
		return DateUtils.format(format, calendar.getTime());
	}

}
